package com.zzyl.job;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Description DeviceDataCleanupTask
 * @Author Lwj
 * @Date 2024-09-14-15:05
 */
@Component
public class DeviceDataCleanupTask {
    @Autowired
    private DataSource dataSource;

    // 每天凌晨1点执行一次
    @Scheduled(cron = "0 0 22 * * ?")
    // 每天五秒执行一次
//    @Scheduled(fixedRate = 5000)
    public void cleanupOldData() {
        String sql = "DELETE FROM device_data WHERE alarm_time < date_sub(now(), interval 30 day )";

        try (Connection connection = dataSource.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
